﻿using Css_VoteSys.Models.Database;
using Css_VoteSys.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Css_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProjectController : ControllerBase
    {
        private readonly Css_VoteSysContext _db;
        private readonly IProjectService _ProjectService;
        private readonly IUserService _UserService;
        private readonly ICategoryService _CategoryService;

        public ProjectController(Css_VoteSysContext db, IProjectService ProjectService, IUserService UserService, ICategoryService CategoryService)
        {
            _db = db;
            _ProjectService = ProjectService;
            _UserService = UserService;
            _CategoryService = CategoryService;
        }

        /// <summary>
        /// 创建投票项目(需登录)
        /// </summary>
        /// <param name="CategoryId">项目类型</param>
        /// <param name="Title">项目名称</param>
        /// <param name="Description">项目描述</param>
        /// <param name="ImageUrl">项目图片地址</param>
        /// <returns></returns>
        [HttpPost]
        public string CreateProject(int CategoryId, string Title, string Description, string ImageUrl)
        {
            // 查询该用户是否创建过该项目 UserId CategoryId
            var UserId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
            if (UserId == null) { return "请先登录"; }
            if (_CategoryService.IsCategory(CategoryId))
            {
                var result = _ProjectService.IsUserHasCategory(Convert.ToInt32(UserId), CategoryId);
                if (result)
                {
                    return "已创建过该类型的项目";
                }
                // 创建项目
                _db.VoteProjects.Add(new VoteProject
                {
                    UserId = Convert.ToInt32(UserId),
                    CategoryId = CategoryId,
                    Title = Title,
                    Description = Description,
                    ImageUrl = ImageUrl,
                    IsShow = 0  //是否展示（0不展示 1展示）
                });
                //保存更改
                var row = _db.SaveChanges();
                if (row > 0)
                    return "创建投票项目成功";
                return "创建投票项目失败";
            }
            return "该项目类型不存在";

        }

        /// <summary>
        /// 查询所有项目(需登录)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<VoteProject> SelectAllProject()
        {
            var UserId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
            if (UserId == null) { return new List<VoteProject>(); }
            var result = _UserService.IsRoot(Convert.ToInt32(UserId));
            // 管理员身份查询所有项目
            if (result)
            {
                return _db.VoteProjects.ToList();
            }

            // 普通用户身份查询 显示项目
            return _db.VoteProjects.Where(x => x.IsShow == 1).ToList();

        }
    }
}
